package main.java.middle;


import java.util.ArrayList;
import java.util.List;

/**
 * @Author: 郑国荣
 * @Date: 2023-09-05-13:47
 * @Description:
 */
public class ThreeSum {
    public List<List<Integer>> threeSum(int[] nums) {
        int numsLen = nums.length;
        if (numsLen < 3) return null;
        List<List<Integer>> result = new ArrayList<>();
        for (int i = 0; i < numsLen; i++) {
            int currentNum = nums[i];
            int left = i + 1;
            int right = numsLen - 1;
            while (left < right) {
                int sum = currentNum + nums[left] + nums[right];
                if (sum == 0) {
                    List<Integer> integers = new ArrayList<>();
                    integers.add(currentNum);
                    integers.add(nums[left]);
                    integers.add(nums[right]);
                    result.add(integers);
                }
                right--;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[] nums = {-1,0,1,2,-1,-4};
        ThreeSum threeSum = new ThreeSum();
        List<List<Integer>> lists = threeSum.threeSum(nums);
        System.out.println(lists.toString());
    }
}
